﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Runtime.InteropServices;

namespace TestPowerSwitch
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        [DllImport("AID.dll")]
        static extern uint FT_ListDevices();
        [DllImport("AID.dll")]
        static extern uint FT_Open();
        [DllImport("AID.dll")]
        static extern uint FT_Close();
        [DllImport("AID.dll")]
        static extern uint FT_Write([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
        [DllImport("AID.dll")]
        static extern uint FT_GetStatus(ref ulong rxsize, ref ulong txsize);
        [DllImport("AID.dll")]
        static extern uint FT_SetBitMode(byte mask, byte enable);
        [DllImport("AID.dll")]
        static extern uint FT_Read([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
        [DllImport("AID.dll")]
        static extern uint FT_EE_Read(ref ushort vid, ref ushort pid, ref ushort power);
        [DllImport("AID.dll")]
        static extern uint FT_EE_Program(ushort power);
        [DllImport("AID.dll")]
        static extern uint FT_EE_ProgramToDefault();
        [DllImport("AID.dll")]
        static extern uint KCAN_Send(uint channel, uint id, uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
        [DllImport("AID.dll")]
        static extern uint KCAN_Receive(ref uint channel, ref uint id, ref uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
        [DllImport("AID.dll")]
        static extern uint KCAN_Init(uint baudraute);

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                TextBlock.Text = Convert.ToString(FT_ListDevices());

                byte[] data = new Byte[1];
                char[] delimiter = new Char[1];
                string[] a_str_data = new String[1];
                uint i;

                delimiter[0] = ',';
                string x = "4";
                a_str_data = x.Split(delimiter, 1);
                for (i = 0; i < a_str_data.Length; i++)
                {
                    if (a_str_data[i].Length > 0) data[i] = Convert.ToByte(a_str_data[i], 16);
                    else data[i] = 0;
                }
                uint qty = FT_ListDevices();
                FT_Open();
                FT_Write(data, i);
                FT_Close();

                MessageBox.Show("OK");                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
